using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace QuestFramework
{
    public class Dialog
    {
        private string conversationFileName;
        private XmlDocument conversationFile;
        private XmlNodeList responseNodes;
        private int currentConversationLevel;
        private string requestString;
        private List<string> responseStrings;
        private bool isConversationOver;
        private Game parentGame;
        private string npcName;

        public Dialog(Game game)
        {
            parentGame = game;
            isConversationOver = true;
            npcName = "Unknown";
        }

        public string NpcName
        {
            get { return npcName; }
        }

        public void StartConversation(int conversationID)
        {
            currentConversationLevel = 0;
            requestString = "";
            responseStrings = new List<string>();
            isConversationOver = false;

            conversationFileName = conversationID.ToString() + ".xml";
            conversationFile = new XmlDocument();
            conversationFile.Load("Content\\Conversations\\" + conversationFileName);

            npcName = conversationFile.SelectSingleNode("//Conversation").Attributes["NpcName"].Value;
            DisplayConversation(currentConversationLevel);
        }

        public void SubmitResponse(int responseIndex)
        {
            try
            {
                for (int responseOptions = 0; responseOptions < responseNodes.Count; responseOptions++)
                {
                    if (responseStrings[responseIndex] == responseNodes[responseOptions].InnerText)
                    {
                        currentConversationLevel = int.Parse(responseNodes[responseOptions].Attributes["NextLine"].Value);
                        break;
                    }
                }

                if (currentConversationLevel == -1)
                {
                    isConversationOver = true;
                }
                else
                {
                    DisplayConversation(currentConversationLevel);
                }
            }
            catch
            {
                // Do Nothing
            }
        }

        public bool IsConversationOver
        {
            get { return isConversationOver; }
            set { isConversationOver = value; }
        }

        public string RequestString
        {
            get { return requestString; }
        }

        public List<string> ResponseStrings
        {
            get { return responseStrings; }
        }

        private void DisplayConversation(int conversationLevel)
        {
            responseStrings.Clear();

            requestString = conversationFile.SelectSingleNode("//Conversation/Line[@ID='" + conversationLevel.ToString() + "']/Request").InnerText;
            responseNodes = conversationFile.SelectNodes("//Conversation//Line[@ID='" + conversationLevel.ToString() + "']/Responses/Response");

            for (int responseOptions = 0; responseOptions < responseNodes.Count; responseOptions++)
            {
                responseStrings.Add(responseNodes[responseOptions].InnerText);
            }
        }
    }
}
